package com.slicktechnologies.client.utility;

import java.util.Date;
import java.util.List;
import java.util.Vector;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.simplesoftwares.client.library.appstructure.SuperModel;
import com.simplesoftwares.client.library.appstructure.search.Filter;
import com.simplesoftwares.client.library.appstructure.search.MyQuerry;
import com.simplesoftwares.client.library.composite.OneToManyComposite;
import com.simplesoftwares.client.library.composite.PersonInfoComposite;
import com.simplesoftwares.client.library.mywidgets.ObjectListBox;
import com.slicktechnologies.client.config.categoryconfig.CategoryTypes;
import com.slicktechnologies.client.config.configurations.ConfigTypes;
import com.slicktechnologies.client.login.LoginForm;
import com.slicktechnologies.client.login.LoginPresenter;
import com.slicktechnologies.client.login.staticresources.ImageResources;
import com.slicktechnologies.shared.SalesLineItem;
import com.slicktechnologies.shared.common.businessunitlayer.Branch;
import com.slicktechnologies.shared.common.helperlayer.Config;
import com.slicktechnologies.shared.common.helperlayer.LoginProxy;
import com.slicktechnologies.shared.common.helperlayer.Type;
import com.slicktechnologies.shared.common.personlayer.Employee;
import com.slicktechnologies.shared.common.personlayer.Person;
import com.slicktechnologies.shared.common.productlayer.Category;
import com.slicktechnologies.shared.common.productlayer.SuperProduct;


// TODO: Auto-generated Javadoc
/**
 * Contains Repeted methods which are needed by all Screens.In Future we need to seprate the methods to their 
 * common classes.
 */
public class AppUtility {
	
	/** The active. */
	public static String ACTIVE="Active";
	
	/** The inactive. */
	public static String INACTIVE="In Active";
	
	/**
	 * Instantiates a new app utility.
	 */
	public AppUtility()
	{
	}

	/**
	 * Make object list box live.
	 *
	 * @param olb the olb
	 * @param querryObject the querry object
	 * @param filters the filters
	 */
	public static void makeObjectListBoxLive(ObjectListBox<?> olb,SuperModel querryObject,Filter... filters)
	{
		Vector<Filter>filterVec= new Vector<Filter>();
		
		for(int i=0;i<filters.length;i++)
		{
			filterVec.add(filters[i]);
		}
		
		MyQuerry qu= new MyQuerry(filterVec,querryObject);
		olb.MakeLive(qu);	
	}
	
	/**
	 * Make live config.
	 *
	 * @param olb the olb
	 * @param screen the screen
	 */
	public static void MakeLiveConfig(ObjectListBox<?> olb,Screen screen)
	{
		    Filter filter=new Filter();
		    filter.setQuerryString("type");
		    filter.setIntValue(ConfigTypes.getConfigType(screen));
		   Filter filterStatus=new Filter();
		    filterStatus.setBooleanvalue(true);
		    
		    filterStatus.setQuerryString("status");
		    
		    makeObjectListBoxLive(olb, new Config(), filter,filterStatus);
	}
	
	private static Filter filterOnStatus()
	{
		 Filter statusfilter=new Filter();
		 statusfilter.setQuerryString("status");
		 statusfilter.setBooleanvalue(true);	
		 return statusfilter;
	}
	/**
	 * Make live category config.
	 *
	 * @param olb the olb
	 * @param screen the screen
	 */
	public static void MakeLiveCategoryConfig(ObjectListBox<?> olb,Screen screen)
	{
		    Filter filter=new Filter();
		    filter.setQuerryString("internalType");
		    filter.setIntValue(CategoryTypes.getCategoryInternalType(screen));
		   
		    makeObjectListBoxLive(olb, new com.slicktechnologies.shared.common.helperlayer.ConfigCategory(), filter,filterOnStatus());
	}
		

	/**
	 * Make sales person list box live.
	 *
	 * @param olb the olb
	 */
	public static void makeSalesPersonListBoxLive(ObjectListBox<?> olb)
	{
		MyQuerry qu= new MyQuerry();
		qu.setQuerryObject(new Employee());
		olb.MakeLive(qu);
	}
	
	/**
	 * Make branch list box live.
	 *
	 * @param olb the olb
	 */
	public static void makeBranchListBoxLive(ObjectListBox<?> olb)
	{
		MyQuerry qu= new MyQuerry();
		qu.setQuerryObject(new Branch());
		olb.MakeLive(qu);
	}
	
	/**
	 * Make type list box live.
	 *
	 * @param olb the olb
	 * @param screen the screen
	 */
	public static void makeTypeListBoxLive(ObjectListBox<?> olb,Screen screen)
	{
		Filter filter=new Filter();
	    filter.setQuerryString("internalType");
	    filter.setIntValue(CategoryTypes.getCategoryInternalType(screen));
	    makeObjectListBoxLive(olb, new Type(), filter);
	}
	
	/**
	 * Make product category list box live.
	 *
	 * @param olb the olb
	 */
	public static void makeProductCategoryListBoxLive(ObjectListBox<?> olb)
	{
		MyQuerry qu= new MyQuerry();
		qu.setQuerryObject(new Category());
		olb.MakeLive(qu);
	}


	/**
	 * Customer info composite.
	 *
	 * @param customer the customer
	 * @return the person info composite
	 */
	public static PersonInfoComposite customerInfoComposite(Person customer) {
		MyQuerry querry=new MyQuerry();
		querry.setQuerryObject(customer);
		
		PersonInfoComposite personinfo=new PersonInfoComposite(querry); 
		return personinfo;
	}

		/**
		 * Initiate one to many composite.
		 *
		 * @param otmComposite the otm composite
		 * @param handler the handler
		 */
		public static void initiateOneToManyComposite(OneToManyComposite<?,?,?> otmComposite,ClickHandler handler) 
		{  
		   MyQuerry onequerry = new MyQuerry(new Vector<Filter>(), new Category());
	
		   Filter filtervalue=new Filter();
		   String querrycon="categoryKey"; 
		   filtervalue.setQuerryString(querrycon);
		   Vector<Filter> manyfilter=new Vector<Filter>();
		   manyfilter.add(filtervalue);
		   MyQuerry manyquerry=new MyQuerry(manyfilter, new SuperProduct());
		  // manyquerry.setQuerryObject(new SuperProduct());
		  // manyquerry.getFilters().add(filtervalue);
		   
		   otmComposite.setOneQuerry(onequerry);
		   otmComposite.setManyQuerry(manyquerry);
		   otmComposite.initializeHashMaps();
		   otmComposite.addClickHandler(handler);
	}

		/**
		 * Make live config.
		 *
		 * @param olb the olb
		 * @param category the category
		 */
		public static void MakeLiveConfig(
				ObjectListBox<Config> olb,  SuperModel category) {
			    Filter filter=new Filter();
			    filter.setQuerryString("deleted");
			    filter.setBooleanvalue(false);
			    
			    makeObjectListBoxLive(olb, new Category(), filter);
			
		}

		
		/**
		 * React on add for one to many composite.
		 *
		 * @param otmComposite the otm composite
		 */
		public static void ReactOnAddForOneToManyComposite(OneToManyComposite<? extends SuperModel,? extends SuperModel,SalesLineItem> otmComposite)
		{
			SuperProduct product=(SuperProduct) otmComposite.getManyComboBox().getSelectedItem();
			System.out.println("Product"+product);
			if(product!=null)
			{
			   SalesLineItem lineItem=new SalesLineItem();
			   //lineItem.setPrduct(product);
				otmComposite.getMany().getDataprovider().getList().add(lineItem);
				System.out.println("Product"+product);
				
			}
			
		}

		
		/**
		 * Sets the status list box.
		 *
		 * @param lbstatus the lbstatus
		 * @param list the list
		 */
		public static void setStatusListBox(ListBox lbstatus,List<String> list)
		{
			lbstatus.addItem("--SELECT--");
			for(String temp:list)
				lbstatus.addItem(temp);
		}
		
		public static void createLogin(String name)
		{
			RootPanel rootPanel = RootPanel.get();
			FlowPanel flowpanel = new FlowPanel();
			ImageResources resources = GWT.create(ImageResources.class);
			Image logo = new Image(resources.logo());
			logo.addStyleName("logo");
			Label lblWelcomeToThe = new Label("Welcome To "+name);
			
			lblWelcomeToThe.setStyleName("gwt-Label-LoginTitle");
			flowpanel.add(lblWelcomeToThe);
			
			
			
			//Patch Patch
			
			
			LoginForm login = new LoginForm();
			LoginProxy model =new LoginProxy();
			LoginPresenter presenter=new LoginPresenter(login,model);
			
			flowpanel.getElement().setId("incenter");
			login.panel.addStyleName("background");
		    flowpanel.add(login.panel);
			rootPanel.add(flowpanel);
		}
		
		public static String parseDate(Date date)
		{
			DateTimeFormat format= DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT);
			return format.format(date);
		}

		
		
		

		
		
		
		
		
		

		
}
